<?php
namespace app\common\model;

class Ask extends BaseModel{
	protected $insert = ['answer_time', 'ip'];

	// 回答时间修改器(仅插入时处理)
	public function setAnswerTimeAttr($answer_time = '', $data = []){
		$answer_time = time();
		return $answer_time;
	}
	/**
	 * 显示时间
	 * @param  string $time 创建时间
	 * @param  array  $data 数据记录
	 */
	public function getShowTimeAttr($time = '', $data = array()){
		$time = $time ? $time : $data['create_time'];
		return $time ? date('Y-m-d H:i:s', $time) : '';
	}
	/**
	 * 用户名称
	 * @param  string $user_id 用户ID
	 * @param  array  $data    数据记录
	 */
	public function getUserNameAttr($user_id = '', $data = array()){
		$user_id = $user_id ? $user_id : $data['user_id'];
		$user_name = model('User')->getShowNameAttr($user_id);

		return $user_name ? $user_name : '';
	}
	public function getUserHeadUrlAttr($user_id = '', $data = array()){
		$user_id      = $user_id ? $user_id : $data['user_id'];
		$is_anonymous = $data['is_anonymous'];
		$head_url     = $is_anonymous ? model('File')->get_path('head-default.png') : model('User')->get_user_head_url($user_id);

		return $head_url ? $head_url : '';
	}
	public function getUpdateViewNumAttr($view_num = '', $data = array()){
		$view_num = $view_num ? $view_num : $data['view_num'];
		$ask_id   = $data['ask_id'];
		$this->where(array('ask_id' => $ask_id))->setInc('view_num');

		return $view_num + 1;
	}
	// 最佳答案获取器
	public function getBestInfoAttr($answer_id = '', $data = array()){
		$answer_id = $answer_id ? $answer_id : $data['answer_id'];
		if(!$answer_id){
			return false;
		}
		$info = model('AskAnswer')->where(array('answer_id' => $answer_id))->find();

		return $info;
	}
	/**
	 * 内容修改器
	 * @param string $content [description]
	 * @param array  $data    [description]
	 */
	public function setContentAttr($content = '', $data = array()){
		$content = $content ? $content : $data['content'];
		$content = model('File')->html_osspath_local($content);

		return $content ? $content : '';
	}
	/**
	 * 内容获取器
	 * @param  string $content [description]
	 * @param  array  $data    [description]
	 * @return [type]          [description]
	 */
	public function getContentAttr($content = '', $data = array()){
		$content = $content ? $content : $data['content'];
		$content = model('File')->html_file_url_oss($content);

		return $content;
	}
	public function getContentImgAttr($content = '', $data = array()){
		$content = $content ? $content : $data['content'];
		$content = model('File')->html_file_url_oss($content, true);

		return $content;
	}
	/**
	 * 标题获取器
	 * @param  string $title [description]
	 * @param  array  $data  [description]
	 * @return [type]        [description]
	 */
	public function getTitleAttr($title = '', $data = []){
		$title = $title ? $title : $data['title'];
		$title = htmlspecialchars($title);
		return $title;
	}
	// 用户收藏时间获取器
	public function getUserFavTimeAttr($ask_id = 0, $data = []){
		$ask_id  = $ask_id ? $ask_id : $data['ask_id'];
		$user_id = $this->user_id();
		
		$where['user_id']   = $user_id;
		$where['fav_type']  = 'ask_fav';
		$where['object_id'] = $ask_id;
		$time = db('UserFav')->where($where)->value('create_time');

		return $time > 0 ? $time : 0;
	}
	// 用户关注时间获取器
	public function getUserFocusTimeAttr($ask_id = 0, $data = []){
		$ask_id  = $ask_id ? $ask_id : $data['ask_id'];
		$user_id = $this->user_id();
		
		$where['user_id']   = $user_id;
		$where['fav_type']  = 'ask_focus';
		$where['object_id'] = $ask_id;
		$time = db('UserFav')->where($where)->value('create_time');

		return $time > 0 ? $time : 0;
	}
	/**
	 * 更新回答数量
	 * @param  string $ask_id 问题ID
	 */
	public function updateAnswerNum($ask_id = ''){
		if(!$ask_id){
			return false;
		}
		$data['answer_num']  = array('exp', 'answer_num+1');
		$data['answer_time'] = time();
		$this->where(array('ask_id' => $ask_id))->update($data);

		return true;
	}
}